package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.common.expection.CoolServiceException;
import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolGenTableMapper;
import cn.da.shuai.cool.system.model.entity.gen.CoolGenTable;
import cn.da.shuai.cool.system.model.entity.gen.CoolGenTableColumn;
import cn.da.shuai.cool.system.service.service.ICoolGenTableColumnService;
import cn.da.shuai.cool.system.service.service.ICoolGenTableService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务表 服务层实现
 */
@Service
public class CoolGenTableServiceImpl extends CoolServiceImpl<CoolGenTableMapper, CoolGenTable> implements ICoolGenTableService {

    @Autowired
    private ICoolGenTableColumnService genTableColumnService;

    @Override
    public List<CoolGenTable> selectGenTableList(CoolGenTable genTable) {
        return this.list(getWrapper(genTable));
    }

    @Override
    public Page<CoolGenTable> selectGenTablePage(CoolGenTable genTable, Page<CoolGenTable> page) {
        return this.page(page, getWrapper(genTable));
    }

    private LambdaQueryWrapper<CoolGenTable> getWrapper(CoolGenTable genTable) {
        LambdaQueryWrapper<CoolGenTable> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(genTable.getTableName()), CoolGenTable::getTableName, genTable.getTableName())
           .like(StrUtil.isNotBlank(genTable.getTableComment()), CoolGenTable::getTableComment, genTable.getTableComment());
        return lqw;
    }

    @Override
    public List<CoolGenTable> selectDbTableList(CoolGenTable genTable) {
        // TODO: 实现查询数据库表列表的逻辑
        return null;
    }

    @Override
    public Page<CoolGenTable> selectDbTablePage(CoolGenTable genTable, Page<CoolGenTable> page) {
        // TODO: 实现查询数据库表分页列表的逻辑
        return null;
    }

    @Override
    public List<CoolGenTable> selectDbTableListByNames(String[] tableNames) {
        // TODO: 实现根据表名查询数据库表的逻辑
        return null;
    }

    @Override
    public List<CoolGenTable> selectGenTableAll() {
        return this.list();
    }

    @Override
    public CoolGenTable selectGenTableById(Long id) {
        CoolGenTable genTable = this.getById(id);
        setTableFromOptions(genTable);
        return genTable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGenTable(CoolGenTable genTable) {
        String options = genTable.getOptions();
        genTable.setOptions(options);
        int row = this.updateById(genTable) ? 1 : 0;
        if (row > 0) {
            for (CoolGenTableColumn cenTableColumn : genTable.getColumns()) {
                genTableColumnService.updateGenTableColumn(cenTableColumn);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGenTableByIds(Long[] tableIds) {
        this.removeByIds(Arrays.asList(tableIds));
        genTableColumnService.deleteGenTableColumnByIds(tableIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importGenTable(List<CoolGenTable> tableList) {
        // TODO: 实现导入表结构的逻辑
    }

    @Override
    public Map<String, String> previewCode(Long tableId) {
        Map<String, String> dataMap = new HashMap<>();
        // TODO: 实现预览代码的逻辑
        return dataMap;
    }

    @Override
    public byte[] downloadCode(String tableName) {
        // TODO: 实现下载代码的逻辑
        return new byte[0];
    }

    @Override
    public void generatorCode(String tableName) {
        // TODO: 实现生成代码的逻辑
    }

    @Override
    public void synchDb(String tableName) {
        // TODO: 实现同步数据库的逻辑
    }

    @Override
    public byte[] downloadCode(String[] tableNames) {
        // TODO: 实现批量下载代码的逻辑
        return new byte[0];
    }

    @Override
    public void validateEdit(CoolGenTable genTable) {
        if (genTable.getColumns().isEmpty()) {
            throw new CoolServiceException("没有列信息");
        }
        if (StrUtil.isBlank(genTable.getTableName())) {
            throw new CoolServiceException("表名称不能为空");
        }
        if (StrUtil.isBlank(genTable.getTableComment())) {
            throw new CoolServiceException("表描述不能为空");
        }
        if (StrUtil.isBlank(genTable.getClassName())) {
            throw new CoolServiceException("实体类名称不能为空");
        }
        if (StrUtil.isBlank(genTable.getPackageName())) {
            throw new CoolServiceException("生成包路径不能为空");
        }
        if (StrUtil.isBlank(genTable.getModuleName())) {
            throw new CoolServiceException("生成模块名不能为空");
        }
        if (StrUtil.isBlank(genTable.getBusinessName())) {
            throw new CoolServiceException("生成业务名不能为空");
        }
        if (StrUtil.isBlank(genTable.getFunctionName())) {
            throw new CoolServiceException("生成功能名不能为空");
        }
    }

    /**
     * 设置代码生成其他选项值
     *
     * @param genTable 设置后的生成对象
     */
    public void setTableFromOptions(CoolGenTable genTable) {
        if (StrUtil.isNotEmpty(genTable.getOptions())) {
            // TODO: 实现设置代码生成其他选项值的逻辑
        }
    }
}